#include <stdio.h>
double power(double n, int p);
int main(void)
{
    double x, xpow;
    int exp;
    
    printf("Enter a number and the integer power");
    printf(" to which\n the number will be raised. Enter q");
    printf(" to quit.\n");
    while (scanf("%lf%d", &x, &exp) == 2)
    {
        xpow = power(x, exp); // 函数调用
        printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
        printf("Enter next pair of numbers of q to quit.\n");
    }
    printf("Hope you enjoyed this power trip -- bye!\n");

    return 0;
}

double power(double n, int p)
{
    double pow = 1;
    int i;

    if (n == 0)
        return 0;
    if (p == 0)
        return 1;
    if (p > 0)
    {
        for (i = 1; i <= p; i++)
            pow *= n;
    }
    if (p < 0)
    {
        for (i = 1; i <= -p; i++)
            pow *= n;
        return (1 / pow);
    }

    return pow;
}